GPS Map
By Rohan Varma
This tutorial will walk you through displaying a user’s current location on a google map. It will utilize the Google Maps activity option on AndroidStudio.
·
Start a new android studio project. In this example, my project
will be called MapTutorial and will have the package tutorial
· On the Activity selection screen choose Google Maps Activity and then create the project
2. Get Developer Key
· When your project is created, Android Studio will automatically open google_maps_api.xml, telling you that you need a Google Maps API key. It will also give you a link to follow in order to get this key
· Paste that link into your browser and it should take you to a page that looks like this
If Create a new project is not selected, select it from the drop down menu and then click continue
· A little bit after clicking continue, you will be redirected to a page saying that the API is enabled but to use the API you’ll need the right credentials. Click the blue button that says Go to Credentials
·
You will then be redirected to a page that looks like this
It is
possible that the box is already auto filled. If it is, go back to your
google_maps_api.xml and check that whatever is in the box matches the
credentials the xml file provides. If not, copy the credentials from the xml
file into the box.
·
After filling in the box, click Create and you should be
redirected to a page that gives you an API key
Copy the highlighted section and paste it into your google_maps_api.xml file (delete the part where it says YOUR KEY HERE and replace it with the highlighted section)
· Once you do this, you are ready to begin programming. However, in order to ensure that everything is working properly, run your program. If it displays a map with a marker somewhere along the equator, everything is running properly. Otherwise, go back and check if you missed one of the steps. Note that it will probably take Gradle a considerable amount of time to build the project the first time.
3. Display user’s current location
· Displaying a user’s location is fairly easy as all you need to do is add the following code to your onMapReady() method in MapsActivity.java. You should also delete the built in marker.
public void onMapReady(GoogleMap googleMap) {
|
·
Now when you open the application, there should be a blue dot at
your location. If you move around, the blue dot should move and show a small
blue chevron indicating the direction in which you are currently moving. Note
that the blue dot will periodically update its location, so don’t be surprised
if updates are kind of choppy. Additionally, at the top right corner of the
screen, a button will appear that centers the map on your current location.